//
//  LNHConfirmOrderViewModel.m
//  LNH
//
//  Created by cruise on 2018/2/14.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "LNHConfirmOrderViewModel.h"

@interface LNHConfirmOrderViewModel() <LMBaseRequestFeformDelegate, YTKRequestDelegate>

@end

@implementation LNHConfirmOrderViewModel

- (instancetype) initWithParams:(NSDictionary *)params {
    if (self = [super initWithParams:params]) {
        self.cartIds = self.params[@"cartIds"];
        NSLog(@"self.params:%@", self.params[@"cartIds"]);
        self.goodsId = self.params[@"goodsId"];
        self.sku = self.params[@"sku"];
        self.number = self.params[@"number"];
        self.levelId = self.params[@"level"];
        self.districtId = self.params[@"districtId"];
    }
    return self;
}

- (void)lm_initializeForViewModel {
     
}

- (RACCommand *)fetchconfirmDataCommand {
    if (!_fetchconfirmDataCommand) {
        @weakify(self);
        if (self.levelId != nil && ![self.levelId isEqualToString:@""]) {
            CAUpgradeConfirmNoCartApi *upgradeConfirmNoCartApi = [[CAUpgradeConfirmNoCartApi alloc] initGoodsId:self.goodsId Sku:self.sku Level:self.levelId DistrictId:self.districtId InviteCode:@""];
            [upgradeConfirmNoCartApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
                CAUpgradeConfirmNoCartApi *api = (CAUpgradeConfirmNoCartApi *) request;
                if (api.done) {
                    self.shippingStr = [NSString stringWithFormat:@"￥%.2f", [api.responseObject[@"result"][@"order"][@"postage"] floatValue]];
                    self.paymentStr = [NSString stringWithFormat:@"￥%.2f", [api.responseObject[@"result"][@"order"][@"order_total_price"] floatValue]];
                    
                    self.addressDict = api.responseObject[@"result"][@"default_address"];
                    self.dataList = api.responseObject[@"result"][@"order"][@"goods"];
                } else {
                    [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
                }
            } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
                [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
            }];
        } else if (self.cartIds == nil) {
            CAComfirmWithNoCartApi *confirmShopCartApi = [[CAComfirmWithNoCartApi alloc] initGoodsId:self.goodsId Sku:self.sku Number:self.number];
            [confirmShopCartApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
                CAConfirmShopCartApi *api = (CAConfirmShopCartApi *) request;
                if (api.done) {
                    self.shippingStr = [NSString stringWithFormat:@"￥%.2f", [api.responseObject[@"result"][@"order"][@"postage"] floatValue]];
                    self.paymentStr = [NSString stringWithFormat:@"￥%.2f", [api.responseObject[@"result"][@"order"][@"order_total_price"] floatValue]];
                    
                    self.addressDict = api.responseObject[@"result"][@"default_address"];
                    self.dataList = api.responseObject[@"result"][@"order"][@"goods"];
                } else {
                    [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
                }
            } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
                [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
            }];
        } else {
            CAConfirmShopCartApi *confirmShopCartApi = [[CAConfirmShopCartApi alloc] initCartIds:self.cartIds];
            [confirmShopCartApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
                CAConfirmShopCartApi *api = (CAConfirmShopCartApi *) request;
                if (api.done) {
                    self.shippingStr = [NSString stringWithFormat:@"￥%.2f", [api.responseObject[@"result"][@"order"][@"postage"] floatValue]];
                    self.paymentStr = [NSString stringWithFormat:@"￥%.2f", [api.responseObject[@"result"][@"order"][@"order_total_price"] floatValue]];
                    
                    self.addressDict = api.responseObject[@"result"][@"default_address"];
                    self.dataList = api.responseObject[@"result"][@"order"][@"goods"];
                } else {
                    [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
                }
            } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
                [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
            }]; 
        }
    }
    return _fetchconfirmDataCommand;
}

#pragma mark - LMBaseRequestFeformDelegate

- (id)request:(LMBaseRequest *)request reformJSONResponse:(id)jsonResponse
{
    
    NSLog(@"jsonResponse:%@", jsonResponse);
    
    if([request isKindOfClass:CAConfirmShopCartApi.class]){
        // 在这里对json数据进行重新格式化
        self.shippingStr = [NSString stringWithFormat:@"￥%.2f", [jsonResponse[@"order"][@"postage"] floatValue]];
        self.paymentStr = [NSString stringWithFormat:@"￥%.2f", [jsonResponse[@"order"][@"order_total_price"] floatValue]];
        
        self.dataList = jsonResponse[@"order"][@"goods"];
    }
    
    
    return jsonResponse;
}

#pragma mark - YTKRequestDelegate
- (void)requestFinished:(__kindof YTKBaseRequest *)request
{
    // 解析数据
    
    
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request
{
    // do something
}

@end
